import 'package:flutter/material.dart';
import 'package:ribo_base_core/base_models/base_chart_liquidfill_model.dart';
import 'package:ribo_base_core/base_models/base_chart_model.dart';
import 'base_chart.dart';
import 'extensions/extension-liquidfill.dart';

class BaseChartLiquidfill extends StatefulWidget {
  BaseChartLiquidfill({Key key, @required this.model}) : super(key: key);

  final BaseChartLiquidfillModel model;

  @override
  _BaseChartLiquidfillState createState() => _BaseChartLiquidfillState();
}

class _BaseChartLiquidfillState extends State<BaseChartLiquidfill> {
  Map<String, dynamic> _option;

  @override
  Widget build(BuildContext context) {
    _handleModel();

    return Container(
        child: Stack(alignment: AlignmentDirectional.bottomCenter, children: [
          BaseChart(
              model: BaseChartModel(
                  option: this._option,
                  style: widget.model?.style,
                  extensions: [extentsionLiquidFill])),
          if (widget.model?.nameText != null)
            Padding(
              padding: EdgeInsets.only(bottom: widget.model?.nameTextPaddingBottom),
              child: Container(
                height: 10,
                child: FittedBox(
                  fit: BoxFit.contain,
                  child: Text(
                    widget.model?.nameText,
                    style: TextStyle(fontSize: 10, color: widget.model?.nameTextColor),
                  ),
                ),
              ),
            ),
        ]));
  }

  void _handleModel(){
    this._option = {
      'series': [
        {
          ...BaseChartLiquidFillStyle().toJson(),
          'type': 'liquidFill',
          'data': widget.model?.data,
          'label': {
            "fontSize": 16,
            'formatter': widget.model?.labelFormatter?.toJson(),
          },
          ...widget.model?.liquidFillStyle?.toJson()??{}
        }
      ]
    };
  }
}
